{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1e5dbc9-4344-417b-ba43-2f13039ac5f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 依赖库安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "753f48d6-751b-45e8-ae7b-6f8c1d433d41",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mRunning cells with 'fullface' requires ipykernel package.\n",
      "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
      "\u001b[1;31mCommand: 'conda install -n fullface ipykernel --update-deps --force-reinstall'"
     ]
    }
   ],
   "source": [
    "pip install mediapipe  -i https://pypi.douban.com/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a2b5f580-f1da-4dcf-a76f-f5cb4f39ed7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting opencv-python\n",
      "  Downloading https://files.pythonhosted.org/packages/40/93/655af887bafece2a655998f53b9bd21ad94b0627d81d44aef35c79f40de6/opencv-python-4.7.0.72.tar.gz (91.1MB)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: Exception:\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 360, in _error_catcher\n",
      "    yield\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 442, in read\n",
      "    data = self._fp.read(amt)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\cachecontrol\\filewrapper.py\", line 62, in read\n",
      "    data = self.__fp.read(amt)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\http\\client.py\", line 447, in read\n",
      "    n = self.readinto(b)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\http\\client.py\", line 491, in readinto\n",
      "    n = self.fp.readinto(b)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\socket.py\", line 589, in readinto\n",
      "    return self._sock.recv_into(b)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\ssl.py\", line 1052, in recv_into\n",
      "    return self.read(nbytes, buffer)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\ssl.py\", line 911, in read\n",
      "    return self._sslobj.read(len, buffer)\n",
      "socket.timeout: The read operation timed out\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\cli\\base_command.py\", line 178, in main\n",
      "    status = self.run(options, args)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\commands\\install.py\", line 352, in run\n",
      "    resolver.resolve(requirement_set)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\resolve.py\", line 131, in resolve\n",
      "    self._resolve_one(requirement_set, req)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\resolve.py\", line 294, in _resolve_one\n",
      "    abstract_dist = self._get_abstract_dist_for(req_to_install)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\resolve.py\", line 242, in _get_abstract_dist_for\n",
      "    self.require_hashes\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\operations\\prepare.py\", line 347, in prepare_linked_requirement\n",
      "    progress_bar=self.progress_bar\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 886, in unpack_url\n",
      "    progress_bar=progress_bar\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 746, in unpack_http_url\n",
      "    progress_bar)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 954, in _download_http_url\n",
      "    _download_url(resp, link, content_file, hashes, progress_bar)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 683, in _download_url\n",
      "    hashes.check_against_chunks(downloaded_chunks)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\utils\\hashes.py\", line 62, in check_against_chunks\n",
      "    for chunk in chunks:\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 651, in written_chunks\n",
      "    for chunk in chunks:\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\utils\\ui.py\", line 156, in iter\n",
      "    for x in it:\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_internal\\download.py\", line 640, in resp_read\n",
      "    decode_content=False):\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 494, in stream\n",
      "    data = self.read(amt=amt, decode_content=decode_content)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 459, in read\n",
      "    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\contextlib.py\", line 130, in __exit__\n",
      "    self.gen.throw(type, value, traceback)\n",
      "  File \"c:\\ProgramData\\Anaconda3\\lib\\site-packages\\pip\\_vendor\\urllib3\\response.py\", line 365, in _error_catcher\n",
      "    raise ReadTimeoutError(self._pool, None, 'Read timed out.')\n",
      "pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.\n"
     ]
    }
   ],
   "source": [
    "pip install opencv-python  -i https://pypi.douban.com/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5cbd119-dfd3-4c77-b93a-2b69903e08b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 摄像头测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1fb7d83-1662-496b-8d98-e84a8bc88057",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mRunning cells with 'fullface' requires ipykernel package.\n",
      "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
      "\u001b[1;31mCommand: 'conda install -n fullface ipykernel --update-deps --force-reinstall'"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "cap = cv2.VideoCapture(0)\n",
    "#cap = cv2.VideoCapture('test.mp4')\n",
    "cap.set(3, 1080) #width\n",
    "cap.set(4, 2244) #height\n",
    "\n",
    "while cap.isOpened():\n",
    "    ret, frame = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    cv2.imshow('WebCam', frame)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc1a94df-976e-4826-92fc-e66cac475b2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# mediapipe测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a50612cc-1d64-4e65-a343-152d127013ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import mediapipe as mp\n",
    "\n",
    "mp_drawing = mp.solutions.drawing_utils\n",
    "mp_holistic = mp.solutions.holistic\n",
    "\n",
    "cap = cv2.VideoCapture(0)\n",
    "#cap = cv2.VideoCapture('test.mp4')\n",
    "\n",
    "with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:\n",
    "    while cap.isOpened():\n",
    "        ret, frame = cap.read()\n",
    "        if not ret:\n",
    "            break\n",
    "    \n",
    "        # recolor\n",
    "        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
    "        image_height, image_width, _ = image.shape\n",
    "        \n",
    "        # Detections\n",
    "        results = holistic.process(image)\n",
    "        \n",
    "        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
    "        \n",
    "        # Face, FACEMESH_CONTOURS / FACEMESH_IRISES / FACEMESH_TESSELATION\n",
    "        mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_TESSELATION)\n",
    "        \n",
    "\n",
    "        # Left Hand\n",
    "        mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)\n",
    "\n",
    "\n",
    "        # Right hand\n",
    "        mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)\n",
    "\n",
    "        # Pose Detections\n",
    "        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)\n",
    "                        \n",
    "        cv2.imshow('Webcam', image)\n",
    "\n",
    "    \n",
    "        if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "            break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fd79301-9bce-4e39-8e47-c4163e753608",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通信：建议使用TCP，包比较大的话UDP发不了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1251de3d-e3ba-4c3d-bea5-fec72d12540c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "from socket import *\n",
    "\n",
    "HOST = ''\n",
    "PORT = 21567\n",
    "BUFSIZ = 1024\n",
    "ADDR = (HOST, PORT)\n",
    "\n",
    "tcpSerSock = socket(AF_INET, SOCK_STREAM)\n",
    "tcpSerSock.bind(ADDR)\n",
    "tcpSerSock.listen(8)\n",
    "\n",
    "\n",
    "def thread_it(func, *args):\n",
    "    '''将函数打包进线程'''\n",
    "    t = threading.Thread(target=func, args=args)\n",
    "    t.setDaemon(False)\n",
    "    t.start()\n",
    "\n",
    "\n",
    "def acceptSuc(tcpCliSock, addr):\n",
    "    print('有一个客户端连接成功')\n",
    "    while True:\n",
    "        try:\n",
    "            recv_data = tcpCliSock.recv(BUFSIZ)\n",
    "        except ConnectionResetError:\n",
    "            print('有一个客户端关闭了连接')\n",
    "        if recv_data:\n",
    "            data = recv_data.decode('utf-8')\n",
    "            print('接收到%s' % data)\n",
    "            tcpCliSock.send((data + \" received\").encode())\n",
    "\n",
    "\n",
    "while True:\n",
    "    print('waiting for connection...')\n",
    "    tcpCliSock, addr = tcpSerSock.accept()\n",
    "    print('...connnecting from:', addr)\n",
    "    thread_it(acceptSuc, tcpCliSock, addr)\n",
    "    print(tcpCliSock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92493819-cc36-4039-af3d-02a168c837cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#下面是Unity端的测试用例 不用在此运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcf1dbc2-f606-4f55-b6ff-7224e31db6a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "using System.Collections;\n",
    "using System.Collections.Generic;\n",
    "using UnityEngine;\n",
    "using System.Net;\n",
    "using System.Net.Sockets;\n",
    "using System.Text;\n",
    "using System.Threading;\n",
    "\n",
    "\n",
    "public class TcpClientRename : MonoBehaviour\n",
    "{\n",
    "    public static IPAddress ip; \n",
    "    IPEndPoint ipEnd;\n",
    "    Thread connectThread;\n",
    "    public static Socket serverSocket;\n",
    "    string recvStr; //接收的字符串\n",
    "    string sendStr; //发送的字符串\n",
    "    public static byte[] sendData = new byte[1024];//接收的数据，必须为字节\n",
    "    byte[] recvData = new byte[1024];//发送的数据，必须为字节\n",
    "    int recvLen; //接收的数据长度\n",
    "\n",
    "    string editString = \"\";\n",
    "\n",
    "    int deb;\n",
    "\n",
    "    void InitSocket()\n",
    "    {\n",
    "        // ip=IPAddress.Parse(\"x.x.x.x\");\n",
    "        ip = IPAddress.Parse(\"127.0.0.1\");\n",
    "\n",
    "        ipEnd = new IPEndPoint(ip, 21567);\n",
    "        connectThread = new Thread(new ThreadStart(SocketReceive));\n",
    "        connectThread.Start();\n",
    "\n",
    "    }\n",
    "\n",
    "    void SocketConnet()\n",
    "    {\n",
    "        if (serverSocket != null)\n",
    "            serverSocket.Close();\n",
    "        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n",
    "        Debug.Log(\"Ready to connect.\");\n",
    "\n",
    "        serverSocket.Connect(ipEnd);\n",
    "\n",
    "    }\n",
    "\n",
    "\n",
    "    static string UTF8String(string input)\n",
    "    {\n",
    "        UTF8Encoding utf8 = new UTF8Encoding();\n",
    "        return utf8.GetString(utf8.GetBytes(input));\n",
    "    }\n",
    "\n",
    "    public static void SocketSend(string sendStr)\n",
    "    {\n",
    "        Debug.Log(sendStr);\n",
    "        TcpClientRename.sendData = new byte[1024];\n",
    "        TcpClientRename.sendData = Encoding.ASCII.GetBytes(sendStr);\n",
    "        serverSocket.Send(TcpClientRename.sendData, TcpClientRename.sendData.Length, SocketFlags.None);\n",
    "    }\n",
    "\n",
    "    void SocketReceive()\n",
    "    {\n",
    "        SocketConnet();\n",
    "        while (true)\n",
    "        {\n",
    "            recvData = new byte[1024];\n",
    "            recvLen = serverSocket.Receive(recvData);\n",
    "            if (recvLen == 0)\n",
    "            {\n",
    "                SocketConnet();\n",
    "                continue;\n",
    "            }\n",
    "            UTF8Encoding utf8 = new UTF8Encoding();\n",
    "            recvStr = utf8.GetString(recvData, 0, recvLen);\n",
    "            Debug.Log(recvStr);\n",
    "        }\n",
    "    }\n",
    "\n",
    "    void SocketQuit()\n",
    "    {\n",
    "        //关闭线程\n",
    "        if (connectThread != null)\n",
    "        {\n",
    "            connectThread.Interrupt();\n",
    "            connectThread.Abort();\n",
    "        }\n",
    "        //最后关闭服务器\n",
    "        if (serverSocket != null)\n",
    "            serverSocket.Close();\n",
    "        print(\"diconnect\");\n",
    "    }\n",
    "\n",
    "    void Start()\n",
    "    {\n",
    "        InitSocket();\n",
    "    }\n",
    "\n",
    "    void OnApplicationQuit()\n",
    "    {\n",
    "        SocketQuit();\n",
    "    }\n",
    "\n",
    "    void OnGUI()\n",
    "    {\n",
    "        int xpos = 10;\n",
    "        editString = GUI.TextField(new Rect(xpos, 10, 500, 20), editString);\n",
    "        string split = ((char)1).ToString();\n",
    "        if (GUI.Button(new Rect(xpos, 30, 60, 20), \"send\"))\n",
    "            SocketSend(editString);\n",
    "    }\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50191719-86cd-4ffc-9372-fd96048687ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "#unity驱动伪代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9712697-2894-4fb9-994d-9c5a9c2beb4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "//面部\n",
    "BlendShapeProxy.SetValue(哪个BlendShape, 设置的Value);\n",
    "iris需要设置location\n",
    "//身体\n",
    "设置animator的骨骼旋转和位置"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fullface",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)]"
  },
  "vscode": {
   "interpreter": {
    "hash": "7160e794e8670ef1c2d15e84b225715b18ba24b470f1438e0c19577ac550887d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
